package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.certificate.pojo.CombinedVo;
import com.jinzhi.jzapi.certificate.pojo.ConsultationsVo;
import com.jinzhi.jzweb.dao.ConsultationsDao;
import com.jinzhi.jzweb.domain.ConsultationsDO;
import com.jinzhi.jzweb.domain.ExamRegistrationDO;
import com.jinzhi.jzweb.service.ConsultationsService;
import com.jinzhi.jzweb.service.ExamRegistrationService;
import com.jinzhi.sys.dao.RoleConsultationsDao;
import com.jinzhi.sys.domain.RoleConsultationsDO;

import com.jinzhi.sys.service.UserService;
import lombok.AllArgsConstructor;

import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * <pre>
 * 资讯表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Service
@AllArgsConstructor
public class ConsultationsServiceImpl extends CoreServiceImpl<ConsultationsDao, ConsultationsDO> implements ConsultationsService {

	private final RoleConsultationsDao roleConsultationsMapper;

    @Autowired
    private ExamRegistrationService examRegistrationService;
    @Autowired
    private UserService userService;
    @Override
    public Page<ConsultationsVo> selectListPage(int current, int number,String firstColumn, String columnName) {

        // 新建分页
        Page<ConsultationsVo> page = new Page<ConsultationsVo>(current, number);

        //定义key

        // 根据情况，返回分页结果
        if(StringUtils.isNotBlank(columnName)){

                    List<ConsultationsVo> list=this.baseMapper.dyGetUserList2(page,firstColumn,columnName);


                    return page.setRecords(list);



        }else{
            if(StringUtils.isNotBlank(firstColumn)){

                    List<ConsultationsVo> list=this.baseMapper.dyGetUserList3(page,firstColumn);


                    return page.setRecords(list);

            }else{


                    List<ConsultationsVo> list=this.baseMapper.dyGetUserList1(page);


                    return page.setRecords(list);

            }

        }
    }
    
    @Override
    public Page<ConsultationsVo> selectListByUserIdPage(int current, int number,String firstColumn, String columnName,Long userId) {

        // 新建分页
        Page<ConsultationsVo> page = new Page<ConsultationsVo>(current, number);

        // 根据情况，返回分页结果
        if(StringUtils.isNotBlank(columnName)){
            return page.setRecords(this.baseMapper.dyGetUserList7(page,firstColumn,columnName,userId));
        }else{
            if(StringUtils.isNotBlank(firstColumn)){
                return page.setRecords(this.baseMapper.dyGetUserList3(page,firstColumn));
            }else{
                return page.setRecords(this.baseMapper.dyGetUserList1(page));
            }

        }
    }
    
    @Transactional
    @Override
    public boolean insert(ConsultationsDO consultations) {

        int count = baseMapper.insert(consultations);
        List<Long> roles = consultations.getRoleIds();
        Long consultationsId = consultations.getId();
        roleConsultationsMapper.removeByConsultationsId(consultationsId);
        List<RoleConsultationsDO> list = new ArrayList<>();
        if (Objects.nonNull(roles)) {
            for (Long roleId : roles) {
                RoleConsultationsDO rc = new RoleConsultationsDO();
                rc.setConsultationsId(consultationsId);
                rc.setRoleId(roleId);
                list.add(rc);
            }
        }
        
        if (list.size() > 0) {
            int i = roleConsultationsMapper.batchSave(list);
        }
        
        return retBool(count);
    }
    
    @Transactional
    @Override
    public boolean update(ConsultationsDO consultations) {

        int count = baseMapper.updateById(consultations);
        List<Long> roles = consultations.getRoleIds();
        Long consultationsId = consultations.getId();
        roleConsultationsMapper.removeByConsultationsId(consultationsId);
        List<RoleConsultationsDO> list = new ArrayList<>();
        if (Objects.nonNull(roles)) {
            for (Long roleId : roles) {
                RoleConsultationsDO rc = new RoleConsultationsDO();
                rc.setConsultationsId(consultationsId);
                rc.setRoleId(roleId);
                list.add(rc);
            }
        }
        
        if (list.size() > 0) {
            int i = roleConsultationsMapper.batchSave(list);
        }
        
        return retBool(count);
    }

    @Override
    public Page<ConsultationsVo> selectThumbnailListPage(int current, int number,String firstColumn, String columnName) {

        // 新建分页
        Page<ConsultationsVo> page = new Page<ConsultationsVo>(current, number);

        // 根据情况，返回分页结果
        if(StringUtils.isNotBlank(columnName)){
            return page.setRecords(this.baseMapper.dyGetUserList5(page,firstColumn,columnName));
        }else{
            if(StringUtils.isNotBlank(firstColumn)){
                return page.setRecords(this.baseMapper.dyGetUserList6(page,firstColumn));
            }else{
                return page.setRecords(this.baseMapper.dyGetUserList4(page));
            }

        }
    }

    @Override
    public Page<ConsultationsDO> findList(int current, int number, String secondColumn) {

        // 新建分页
        Page<ConsultationsDO> page = new Page<ConsultationsDO>(current, number);

        return page.setRecords(this.baseMapper.findList(page , secondColumn));
    }

    @Override
    public Page<CombinedVo> selNewsList1(int current, int number, String firstColumn, String columnName) {
        // 分页对象
        Page<CombinedVo> combinedPage = new Page<>(current, number);
        List<CombinedVo> combinedRecords = new ArrayList<>();

        // 获取ConsultationsVo的数据（所有记录）
        List<ConsultationsVo> consultationsList;
        if (StringUtils.isNotBlank(columnName)) {
            consultationsList = this.baseMapper.dyGetUserList2(new Page<>(1, Integer.MAX_VALUE), firstColumn, columnName);
        } else if (StringUtils.isNotBlank(firstColumn)) {
            consultationsList = this.baseMapper.dyGetUserList3(new Page<>(1, Integer.MAX_VALUE), firstColumn);
        } else {
            consultationsList = this.baseMapper.dyGetUserList1(new Page<>(1, Integer.MAX_VALUE));
        }

        // 获取ExamRegistrationDO的数据（所有记录）
        Wrapper<ExamRegistrationDO> wrapper = new EntityWrapper<ExamRegistrationDO>().orderBy("id", false);
        List<ExamRegistrationDO> examRegistrationList = examRegistrationService.selectList(wrapper);

        // 转换ExamRegistrationDO的类型
        for (ExamRegistrationDO obj : examRegistrationList) {
            switch (obj.getExamType()) {
                case "0":
                    obj.setExamType("1+X幼儿照护职业技能等级证书师资及考评员");
                    break;
                case "1":
                    obj.setExamType("1+X幼儿照护职业技能等级证书高级师资及考评员");
                    break;
                case "2":
                    obj.setExamType("1+X幼儿照护职业技能等级证书考评员到期复审换证");
                    break;
                case "3":
                    obj.setExamType("1+X产后恢复职业技能等级证书师资及考评员");
                    break;
                case "4":
                    obj.setExamType("1+X幼儿照护、产后恢复职业技能等级证书考务技术与督导员");
                    break;
                case "5":
                    obj.setExamType("高职护理——江西国培");
                    break;
                case "6":
                    obj.setExamType("中职幼儿保育——江西国培");
                    break;
                case "7":
                    obj.setExamType("教学能力竞赛研修班");
                    break;
                case "8":
                    obj.setExamType("金砖大赛 - 第二届婴幼儿照护训练营");
                    break;
                case "9":
                    obj.setExamType("金砖大赛-首届产后恢复训练营");
                    break;
                case "10":
                    obj.setExamType("2024年国培省培班");
                    break;
                case "11":
                    obj.setExamType("会议");
                    break;
                case "12":
                    obj.setExamType("1+X幼儿照护、产后恢复职业技能等级证书考务技术员与督导员到期复审换证");
                    break;
                case "13":
                    obj.setExamType("1+X产后恢复职业技能等级证书考评员到期复审换证");
                    break;
                case "14":
                    obj.setExamType("金砖大赛-第二届产后恢复训练营");
                    break;
                case "15":
                    obj.setExamType("金砖大赛-第三届婴幼儿照护训练营");
                    break;
                case "16":
                    obj.setExamType("金砖大赛-首届老年人能力评估训练营");
                    break;
            }
            obj.setUserName(userService.selectById(obj.getUserId()).getUsername());
        }

        // 合并数据
        for (ExamRegistrationDO examRegistration : examRegistrationList) {
            CombinedVo combinedVo = new CombinedVo();
            combinedVo.setExamRegistrationDO(examRegistration);
            combinedRecords.add(combinedVo);
        }
        for (ConsultationsVo consultationsVo : consultationsList) {
            CombinedVo combinedVo = new CombinedVo();
            combinedVo.setConsultationsVo(consultationsVo);
            combinedRecords.add(combinedVo);
        }

        // 设置分页信息
        int start = (current - 1) * number;
        int end = Math.min(start + number, combinedRecords.size());
        combinedPage.setRecords(combinedRecords.subList(start, end));
        combinedPage.setTotal(combinedRecords.size());

        return combinedPage;
    }

}
